package ifmo.compiler.task21.parser;

import ifmo.compiler.task21.parser.symbols.Nonterminal;
import ifmo.compiler.task21.parser.symbols.Symbol;

import java.util.HashMap;

public class PredictiveAnalysTable {
	private HashMap<Nonterminal, HashMap<Symbol, Production>> _map;

	public PredictiveAnalysTable() {
		_map = new HashMap<Nonterminal, HashMap<Symbol, Production>>();
	}

	public void InsertProduction(Nonterminal key1, Symbol key2, Production value) throws Exception {
		HashMap<Symbol, Production> map;

		if (_map.keySet().contains(key1)) {
			map = _map.get(key1);
		} else {
			map = new HashMap<Symbol, Production>();
			_map.put(key1, map);
		}

		if (!map.containsKey(key2))
		{
			map.put(key2, value);
		} else {
			throw new Exception("Table already contains instance for this keys!");
		}
	}
	
	public Production getProduction(Nonterminal key1, Symbol key2)
	{
		if (_map.keySet().contains(key1)) {
			HashMap<Symbol, Production> map = _map.get(key1);
			
			if (map.keySet().contains(key2))
			{
				return map.get(key2);
			}
		}
		
		return null;
	}
}
